<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use function fast\array_get;

/**
 * 云途物流收费查询
 *
 * @icon fa fa-circle-o
 */
class Logisticsyt extends Backend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * Logisticsyt模型对象
     * @var \app\admin\model\Logisticsyt
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Logistics;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $filter = $filter ? $filter : [];
            $data = array();
            if (!empty($filter)) {
                $url = 'https://www.yunexpress.cn/Help/PriceQuery';
                if (!empty($filter['SenderCityName'])) {
                    $QueryFreight['SenderCityCode'] = $filter['SenderCityName'];
                }
                $QueryFreight['CountryCode'] = $filter['CountryName'];
                $QueryFreight['IsHome'] = '1';
                if (!empty($filter['ProductGroupCode'])) {
                    $QueryFreight['ProductGroupCode'] = $filter['ProductGroupCode'];
                }
                if (!empty($filter['PackageType'])) {
                    $QueryFreight['PackageType'] = $filter['PackageType'];
                }
                $QueryFreight['Weight'] = $filter['Weight'];
                $QueryFreight['Length'] = $filter['Length'];
                $QueryFreight['Width'] = $filter['Width'];
                $QueryFreight['Height'] = $filter['Height'];
                $QueryFreight['Sort'] = '0';
                $post['QueryFreight'] = $QueryFreight;
                $data = $this->posturl($url,$post);
                if($data){
                    $data = json_decode($data,true);
                    if ($data['isSuccess'] == true) {
                        $List = $data['List'];
                    }
                }
            }
            $result = array("total" => count($List), "rows" => $List);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                $this->error('重量必须为大于0的数字，最多2位小数');
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    //type 类型 operation具体操作
//                    $this->add_virtualmachine_log(2,$result,1);
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    public function posturl($url,$data = array()){
        $data = (is_array($data)) ? json_encode($data,JSON_UNESCAPED_UNICODE) : $data;
        $headerArray = array("Content-Type:application/json;");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}
